from code.data.Subject import Subject
from code.data.Comment import Comment
from code.data.User import User
from code.dao.user_management import search_user
from code.dao.comment_management import add_comment,delete_comment,search_comment,search_comment_by_subjectID
from code.dao.subject_management import add_subject,delete_subject,search_subject


class SubjectBackend:
    @staticmethod
    def subject_announce(userin:User, subjectin:Subject):
        ret = 0
        if userin.userID is None:
            ret = 5001
        elif subjectin.content is None:
            ret = 5002
        else:
            user_list = search_user(userin, "userID")[0]
            if len(user_list) is None:
                ret = 5003
            else:
                user = user_list[0]
                if add_subject(Subject(content=subjectin.content,userID=subjectin.userID,)) is True:
                    ret = 1
                else:
                    ret = 0
        return ret

    @staticmethod
    def subject_delete(userin:User,subjectin:Subject):
        ret = 0
        if userin.userID is None:
            ret = 5001
        elif subjectin.subjectID is None:
            ret = 5009
        else:
            user_list = search_user(userin, "userID")
            subject = search_subject(subjectin,"subjectID")[0]
            if len(user_list) is None:
                ret = 5003
            else:
                user = user_list[0]

            if subject is None:
                ret = 5004
            elif user.type == "Teacher" or user.type == "admin":
                if delete_subject(subject) is True:
                    ret = 1
                else:
                    ret = 0
            elif user.userID != subject.userID:
                ret = 5005
            else:
                if delete_subject(subject) is True:
                    ret = 1
                else:
                    ret = 0
        return ret

    @staticmethod
    def subject_display(userin:User):
        ret = 0
        if userin.userID is None:
            ret = 5001
        else:
            user_list = search_user(userin, "userID")
            subject_back = search_subject(Subject(),"all")
            if len(user_list) is None:
                ret = 5003
            else:
                user = user_list[0]

            if subject_back is None or len(subject_back) ==0:
                ret = 5014
            else:
                ret = subject_back
        return ret


if __name__ == '__main__':
    ret = []
    print(len(ret))